package com.example.yurqi.webviewexample

import android.annotation.SuppressLint
import android.os.Build
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.annotation.RequiresApi
import android.view.View
import android.webkit.*
import android.widget.Toast
import com.example.yurqi.webviewexample.model.Person
import kotlinx.android.synthetic.main.activity_main.*
import me.ezitku.JsBridge.JsBridge
import kotlin.jvm.*
import kotlin.jvm.internal.Reflection
import kotlin.reflect.jvm.reflect

class MainActivity : AppCompatActivity() {
    @SuppressLint("JavascriptInterface")
    @RequiresApi(Build.VERSION_CODES.KITKAT)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        webview.loadUrl("file:///android_asset/web/index.html")
//        webview.loadUrl("http://www.html5test.com")
        var settings = webview.settings
        settings.javaScriptEnabled = true
        webview.addJavascriptInterface(this, "main")
        webview.addJavascriptInterface(JSHook(this), "hook")
        webview.setWebChromeClient(object : WebChromeClient() {
            override fun onShowCustomView(view: View?, callback: CustomViewCallback?) {
                super.onShowCustomView(view, callback)
            }
        })
        webview.setWebViewClient(object : WebViewClient() {
            override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest?): Boolean {
                return super.shouldOverrideUrlLoading(view, request)
            }

        })

        var jsFunc = JsBridge(JsFunc::class.java, webview).instance<JsFunc>()

        html.setOnClickListener {
            jsFunc.getName {
                Toast.makeText(this, it, Toast.LENGTH_LONG).show()
            }
        }
        htmlParam.setOnClickListener {
            var p = Person()
            p.name = "Java"
            p.age = 21
            p.sex = 10
            jsFunc.setName(p)
        }

        normal.setOnClickListener {
            //jsFunc.normal()
            jsFunc.list {
                Toast.makeText(this, it[0].name, Toast.LENGTH_LONG).show()
            }
        }


    }

}
